////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2009 Daniel F. Hanson
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
////////////////////////////////////////////////////////////////////////////////

using System.Collections.Generic;

namespace SilverLua.Compiler
{
   internal class AssignStatementNode : StatementNode
   {
      private VariableNode[] _varList;
      private ExpressionNode[] _exprList;

      internal AssignStatementNode(VariableNode[] varList, ExpressionNode[] exprList)
      {
         _varList = varList;
         _exprList = exprList;
      }

      internal override void GenerateInstructions(Parser parser, int currentChunk)
      {
         if (_varList.Length == 1 && _exprList.Length == 1)
         {
            _varList[0].GenerateAssign(_exprList[0].Evaluate(parser, currentChunk),
                                       parser, currentChunk);
         }
         else
         {
            int length = _varList.Length < _exprList.Length ? _varList.Length : _exprList.Length;
            int[] tempRegisters = new int[length];
            
            for (int i = 0; i < length; ++i)
            {
               tempRegisters[i] = parser.ChunkList[currentChunk].AllocateTempRegister();
            }

            for (int i = 0; i < length; ++i)
            {
               _exprList[i].EvaluateTo(tempRegisters[i], parser, currentChunk);
            }

            for (int i = 0; i < length; ++i)
            {
               _varList[i].GenerateAssign(tempRegisters[i], parser, currentChunk);
            }
         }
         
         if (_varList.Length > _exprList.Length)
         {
            for (int i = _exprList.Length; i < _varList.Length; ++i)
            {
               _varList[i].GenerateAssign(new NilLiteralNode().Evaluate(parser, currentChunk),
                                          parser, currentChunk);
            }
         }
      }
   }
}
